#!/bin/bash

# 输出姓名和学号
echo "姓名：何臣飞"
echo "学号：24055060343"

# 进入students文件夹
cd students

# 设置性别筛选条件（根据你的性别修改）
my_gender="M"  # 如果你是男生用"M"，女生用"F"

# 初始化变量
total_average=0
count=0

echo "筛选性别为 $my_gender 的学生..."

# 遍历所有学生文件
for file in *.txt; do
    # 读取性别信息
    gender=$(grep "gender:" "$file" | awk '{print $2}')
    
    # 如果性别匹配，则提取平均分
    if [[ "$gender" == "$my_gender" ]]; then
        average=$(grep "average:" "$file" | awk '{print $2}')
        echo "学生 $(basename "$file" .txt): 平均分 $average"
        total_average=$(echo "scale=1; $total_average + $average" | bc)
        count=$((count + 1))
    fi
done

# 计算总平均分
if [[ $count -gt 0 ]]; then
    overall_average=$(echo "scale=1; $total_average / $count" | bc)
    echo ""
    echo "Average score of ${my_gender}($([ "$my_gender" == "M" ] && echo "Male" || echo "Female")): $overall_average"
else
    echo "没有找到性别为 $my_gender 的学生"
fi
